package com.findyou.dao.Impl;

import com.findyou.dao.AccountDao;
import com.findyou.utils.ConnectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

/**
 * @Title: AccountDaoImpl
 * @Author 贾金博
 * @Package com.findyou.dao.Impl
 * @Date 2024/12/6 上午9:13
 * @description: 实现类
 */
@Repository
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private QueryRunner queryRunner;

    @Autowired
    private ConnectionUtils connectionUtils;

    /**
     * 转出
     * @param outUser
     * @param money
     */
    @Override
    public void out(String outUser, Double money) {
        String sql = "update account set money = money - ? where name = ?";
        try {
            queryRunner.update(connectionUtils.getThreadConnection(), sql, money, outUser);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 转入
     * @param inUser
     * @param money
     */
    @Override
    public void in(String inUser, Double money) {
        String sql = "update account set money = money + ? where name = ?";
        try {
            queryRunner.update(connectionUtils.getThreadConnection(), sql, money, inUser);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
